<!DOCTYPE html>
<html>
<head><meta charset="utf-8" />
<title>201-语句</title>

<script src="/lib/require/index.js"></script>
<script src="/lib/jquery/index.js"></script>

<!-- 改为引入外部样式 -->
<link rel="stylesheet" href="/css/custom.css">

<!-- Loading mathjax macro -->
<!-- Load mathjax -->
    <script src="/lib/mathjax/index.js"></script>
    <!-- MathJax configuration -->
    <script type="text/x-mathjax-config">
    MathJax.Hub.Config({
        tex2jax: {
            inlineMath: [ ['$','$'], ["\\(","\\)"] ],
            displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
            processEscapes: true,
            processEnvironments: true
        },
        // Center justify equations in code and markdown cells. Elsewhere
        // we use CSS to left justify single line equations in code cells.
        displayAlign: 'center',
        "HTML-CSS": {
            styles: {'.MathJax_Display': {"margin": 0}},
            linebreaks: { automatic: true }
        }
    });
    </script>
    <!-- End of mathjax configuration --></head>
<body>
<div class="toc">
<ul>
                        
                        
                        <li class="toc-li-2">
                            <a href="#语句">语句</a>
                        </li>
                    
                
            
            
            
            
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#內建语句">內建语句</a>
                        </li>
                    
                
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#import">import</a>
                        </li>
                    
                
            
            
            
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#赋值语句">赋值语句</a>
                        </li>
                    
                
            
            
            
            
            
            
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#if条件语句">if条件语句</a>
                        </li>
                    
                
            
            
            
            
            
            
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#if-...-elif-...-else">if ... elif ... else</a>
                        </li>
                    
                
            
            
            
            
            
            
            
            
            
            
            
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#三元操作符">三元操作符</a>
                        </li>
                    
                
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#for循环">for循环</a>
                        </li>
                    
                
            
            
            
            
            
            
            
            
            
                        
                        
                        <li class="toc-li-4">
                            <a href="#range(start,stop[,-step])--->-range-object">range(start,stop[, step]) --> range object</a>
                        </li>
                    
                
            
            
            
            
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#while循环">while循环</a>
                        </li>
                    
                
            
            
            
            
            
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#break语句">break语句</a>
                        </li>
                    
                
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#continue语句">continue语句</a>
                        </li>
                    
                
            
            
                        
                        
                        <li class="toc-li-3">
                            <a href="#pass语句">pass语句</a>
                        </li>
                    
                
            
            </ul>
</div>
  <div tabindex="-1" id="notebook" class="border-box-sizing">
    <div class="container" id="notebook-container">

<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[1]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="c1"># 打印所有单行变量</span>
<span class="kn">from</span> <span class="nn">IPython.core.interactiveshell</span> <span class="k">import</span> <span class="n">InteractiveShell</span>
<span class="n">InteractiveShell</span><span class="o">.</span><span class="n">ast_node_interactivity</span> <span class="o">=</span> <span class="s1">&#39;all&#39;</span>
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="&#35821;&#21477;">&#35821;&#21477;<a class="anchor-link" href="#&#35821;&#21477;">&#182;</a></h2><p>一般所有高级语言，都包含如下语句，Python也不例外：</p>
<ul>
<li>循环语句:容许一些语句反复运行数次。循环可依据一个默认的数目来决定运行这些语句的次数；或反复运行它们，直至某些条件改变。</li>
<li>条件语句:容许仅当某些条件成立时才运行某个区块。否则，这个区块中的语句会略去，然后按区块后的语句继续运行。</li>
<li>无条件分支语句容许运行顺序转移到程序的其他部分之中。包括跳跃（在很多语言中称为Goto）、副程序和Procedure等。</li>
</ul>
<p>循环、条件分支和无条件分支都是控制流程。</p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="&#20839;&#24314;&#35821;&#21477;">&#20839;&#24314;&#35821;&#21477;<a class="anchor-link" href="#&#20839;&#24314;&#35821;&#21477;">&#182;</a></h3>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[2]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">keyword</span>
<span class="n">keyword</span><span class="o">.</span><span class="n">kwlist</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[2]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>[&#39;False&#39;,
 &#39;None&#39;,
 &#39;True&#39;,
 &#39;and&#39;,
 &#39;as&#39;,
 &#39;assert&#39;,
 &#39;break&#39;,
 &#39;class&#39;,
 &#39;continue&#39;,
 &#39;def&#39;,
 &#39;del&#39;,
 &#39;elif&#39;,
 &#39;else&#39;,
 &#39;except&#39;,
 &#39;finally&#39;,
 &#39;for&#39;,
 &#39;from&#39;,
 &#39;global&#39;,
 &#39;if&#39;,
 &#39;import&#39;,
 &#39;in&#39;,
 &#39;is&#39;,
 &#39;lambda&#39;,
 &#39;nonlocal&#39;,
 &#39;not&#39;,
 &#39;or&#39;,
 &#39;pass&#39;,
 &#39;raise&#39;,
 &#39;return&#39;,
 &#39;try&#39;,
 &#39;while&#39;,
 &#39;with&#39;,
 &#39;yield&#39;]</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="import">import<a class="anchor-link" href="#import">&#182;</a></h3><p>导入模块</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[3]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">math</span>  <span class="c1"># 可读性强</span>
<span class="n">math</span><span class="o">.</span><span class="n">pow</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[3]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>9.0</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>导入模块中单个或多个方法</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[4]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">math</span> <span class="k">import</span> <span class="nb">pow</span>  <span class="c1"># 可读性差</span>
<span class="kn">from</span> <span class="nn">math</span> <span class="k">import</span> <span class="n">e</span><span class="p">,</span> <span class="n">pi</span>

<span class="nb">pow</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span>   <span class="c1"># --&gt;</span>
<span class="nb">pow</span><span class="p">(</span><span class="n">e</span><span class="p">,</span><span class="n">pi</span><span class="p">)</span>  <span class="c1"># --&gt;</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[4]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>9.0</pre>
</div>

</div>

<div class="output_area">

<div class="prompt output_prompt">Out[4]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>23.140692632779263</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>导入模块中的函数并自定义名称</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[5]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">math</span> <span class="k">import</span> <span class="nb">pow</span> <span class="k">as</span> <span class="n">pingfang</span>
<span class="n">pingfang</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[5]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>9.0</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>导入模块中的所有方法</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[6]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">math</span> <span class="k">import</span> <span class="o">*</span>

<span class="nb">pow</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span>  <span class="c1"># --&gt;</span>
<span class="n">sqrt</span><span class="p">(</span><span class="mi">9</span><span class="p">)</span>   <span class="c1"># --&gt;</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[6]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>9.0</pre>
</div>

</div>

<div class="output_area">

<div class="prompt output_prompt">Out[6]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>3.0</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="&#36171;&#20540;&#35821;&#21477;">&#36171;&#20540;&#35821;&#21477;<a class="anchor-link" href="#&#36171;&#20540;&#35821;&#21477;">&#182;</a></h3><blockquote><p>编程中的“=”和数学中的“=”是完全不同的。在编程语言中，“=”表示“赋值”过程。<br>
这里的赋值不是把右边的值给左边，只是在右边的对象上贴一张标签，实际操作的是右边的对象<br>
具体看<a href="./106-数据存储原理.html">python数据存储原理</a></p>
</blockquote>
<p>多重赋值语句</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[7]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span> <span class="o">=</span> <span class="mi">1</span><span class="p">,</span> <span class="s2">&quot;python&quot;</span><span class="p">,</span> <span class="p">[</span><span class="s2">&quot;hello&quot;</span><span class="p">,</span> <span class="s2">&quot;world&quot;</span><span class="p">]</span>

<span class="n">x</span>  <span class="c1"># --&gt;</span>
<span class="n">y</span>  <span class="c1"># --&gt;</span>
<span class="n">z</span>  <span class="c1"># --&gt;</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[7]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>1</pre>
</div>

</div>

<div class="output_area">

<div class="prompt output_prompt">Out[7]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>&#39;python&#39;</pre>
</div>

</div>

<div class="output_area">

<div class="prompt output_prompt">Out[7]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>[&#39;hello&#39;, &#39;world&#39;]</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>变量值互换</p>
<blockquote><p>用之前学的<a href="./106-数据存储原理.html">python数据存储原理</a>来理解</p>
</blockquote>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[8]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="mi">2</span>
<span class="n">b</span> <span class="o">=</span> <span class="mi">9</span>
<span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">b</span><span class="p">,</span> <span class="n">a</span>  <span class="c1"># 相当于标签互换</span>

<span class="n">a</span>  <span class="c1"># --&gt;</span>
<span class="n">b</span>  <span class="c1"># --&gt;</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[8]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>9</pre>
</div>

</div>

<div class="output_area">

<div class="prompt output_prompt">Out[8]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>2</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>还有一种赋值方式，被称为“链式赋值”</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[9]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">m</span> <span class="o">=</span> <span class="n">n</span> <span class="o">=</span> <span class="s2">&quot;python&quot;</span>

<span class="n">m</span>  <span class="c1"># --&gt;</span>
<span class="n">n</span>  <span class="c1"># --&gt;</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[9]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>&#39;python&#39;</pre>
</div>

</div>

<div class="output_area">

<div class="prompt output_prompt">Out[9]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>&#39;python&#39;</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="if&#26465;&#20214;&#35821;&#21477;">if&#26465;&#20214;&#35821;&#21477;<a class="anchor-link" href="#if&#26465;&#20214;&#35821;&#21477;">&#182;</a></h3><p>基本语法:</p>

<pre><code>if bool(conj):      # 如果if后面的条件满足，则执行后面的语句块
    do something    # 统一缩进4个空格</code></pre>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="if-...-elif-...-else">if ... elif ... else<a class="anchor-link" href="#if-...-elif-...-else">&#182;</a></h4><p>基本样式结构：</p>

<pre><code>if 条件1:     # 满足条件1则执行语句块1
    语句块1
elif 条件2:   # 满足条件2则执行语句块2
    语句块2
elif 条件3：
    语句块3
...
else:       # 上面条件都都不满足才执行
    语句块4</code></pre>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="&#19977;&#20803;&#25805;&#20316;&#31526;">&#19977;&#20803;&#25805;&#20316;&#31526;<a class="anchor-link" href="#&#19977;&#20803;&#25805;&#20316;&#31526;">&#182;</a></h3>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[11]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">x</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
<span class="n">y</span> <span class="o">=</span> <span class="mi">1</span> <span class="k">if</span> <span class="n">x</span> <span class="o">&lt;</span> <span class="mi">0</span> <span class="k">else</span> <span class="mi">2</span>
<span class="n">y</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[11]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>1</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[12]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">x</span> <span class="o">=</span> <span class="mi">2</span>
<span class="n">y</span> <span class="o">=</span> <span class="mi">1</span> <span class="k">if</span> <span class="n">x</span> <span class="o">&lt;</span> <span class="mi">0</span> <span class="k">else</span> <span class="mi">2</span>
<span class="n">y</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[12]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>2</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="for&#24490;&#29615;">for&#24490;&#29615;<a class="anchor-link" href="#for&#24490;&#29615;">&#182;</a></h3><p>其基本结构是：</p>

<pre><code>for 循环规则：      # 冒号:结束
    操作语句       # 缩进四个空格</code></pre>
<p>字符串for循环：</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[13]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">hello</span> <span class="o">=</span> <span class="s2">&quot;world&quot;</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">hello</span><span class="p">:</span>         <span class="c1"># 冒号:结束</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>            <span class="c1"># print自带end=&#39;\n&#39;</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt"></div>


<div class="output_subarea output_stream output_stdout output_text">
<pre>w
o
r
l
d
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p><strong>循环的对象必须是可迭代的对象</strong>，判断是否存在:</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[14]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="s1">&#39;__iter__&#39;</span> <span class="ow">in</span> <span class="nb">dir</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>  <span class="c1"># --&gt;</span>
<span class="s1">&#39;__iter__&#39;</span> <span class="ow">in</span> <span class="nb">dir</span><span class="p">(</span><span class="nb">list</span><span class="p">)</span>  <span class="c1"># --&gt;</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt output_prompt">Out[14]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>False</pre>
</div>

</div>

<div class="output_area">

<div class="prompt output_prompt">Out[14]:</div>




<div class="output_text output_subarea output_execute_result">
<pre>True</pre>
</div>

</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="range(start,stop[,-step])---&gt;-range-object">range(start,stop[, step]) --&gt; range object<a class="anchor-link" href="#range(start,stop[,-step])---&gt;-range-object">&#182;</a></h4><ul>
<li>start：开始数值，缺省则为0</li>
<li>stop：结束的数值，必须要写，取值为stop-1</li>
<li>step：变化的步长，缺省则为1<ul>
<li>stop&gt;start : step&gt;0(非必须)</li>
<li>stop&lt;start : step&lt;0(非必须)</li>
</ul>
</li>
</ul>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="while&#24490;&#29615;">while&#24490;&#29615;<a class="anchor-link" href="#while&#24490;&#29615;">&#182;</a></h3>
<pre><code>while 判断条件:     # 冒号:结束
    语句           # 缩进4个空格
else:
    语句</code></pre>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="break&#35821;&#21477;">break&#35821;&#21477;<a class="anchor-link" href="#break&#35821;&#21477;">&#182;</a></h3><p>跳出 for 和 while 的循环体</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[15]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">5</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
        <span class="k">break</span>  <span class="c1"># 跳出循环</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt"></div>


<div class="output_subarea output_stream output_stdout output_text">
<pre>0
1
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="continue&#35821;&#21477;">continue&#35821;&#21477;<a class="anchor-link" href="#continue&#35821;&#21477;">&#182;</a></h3><p>跳过当前循环块中的剩余语句，然后继续进行下一轮循环</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[16]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">5</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
        <span class="k">continue</span>  <span class="c1"># 跳过当前循环</span>
    <span class="nb">print</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">

<div class="prompt"></div>


<div class="output_subarea output_stream output_stdout output_text">
<pre>0
1
3
4
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="pass&#35821;&#21477;">pass&#35821;&#21477;<a class="anchor-link" href="#pass&#35821;&#21477;">&#182;</a></h3><p>空语句，是为了保持程序结构的完整性，pass不做任何事情，一般用做占位语句</p>

</div>
</div>
</div>
    </div>
  </div>
</body>

 


</html>
